'use strict';

const BaseController = require('./base');

class StudentController extends BaseController {
  constructor(ctx) {
    super(ctx, 'student');
  }

  // 重写 getPageQuery 方法，用于列表筛选
  getPageQuery(query) {
    const { name, student_no, grade, class_name, parent_id, status } = query;
    const where = {};

    if (name) where.name = { [this.app.Sequelize.Op.like]: `%${name}%` };
    if (student_no) where.student_no = { [this.app.Sequelize.Op.like]: `%${student_no}%` };
    if (grade) where.grade = grade;
    if (class_name) where.class_name = class_name;
    if (parent_id) where.parent_id = parent_id;
    if (status) where.status = status;

    return where;
  }

  // 重写 getListQuery 方法，用于全部列表筛选
  getListQuery(query) {
    return this.getPageQuery(query);
  }

  async create() {
    // 相同年级、相同班级的学号不能重复
    const { ctx } = this;
    const { student_no, grade, class_name } = ctx.request.body;
    if (!student_no || !grade || !class_name) {
      ctx.throw(400, '学号、年级、班级为必填项');
    }
    const exists = await this.ctx.service[this.serviceName].findOneWithWhere({
      student_no,
      grade,
      class_name
    });
    if (exists) {
      ctx.throw(400, '该学生已存在');
    }
    const result = await this.ctx.service[this.serviceName].create({
      ...ctx.request.body,
      parent_id: ctx.request.body.parent_id || 0
    });
    ctx.success(result);
  }

  async update() {
    // 除了修改本身 相同年级、相同班级的学号不能重复
    const { ctx } = this;
    const id = ctx.params.id;
    const { student_no, grade, class_name, name } = ctx.request.body;
    if (!student_no || !grade || !class_name || !name) {
      ctx.throw(400, '姓名、学号、年级、班级为必填项');
    }
    const exists = await this.ctx.service[this.serviceName].findOneWithWhere({
      student_no,
      grade,
      class_name,
      id: { [this.app.Sequelize.Op.ne]: id }
    });
    if (exists) {
      ctx.throw(400, '该学生已存在');
    }
    const result = await this.ctx.service[this.serviceName].update(id, {
      ...ctx.request.body,
      parent_id: ctx.request.body.parent_id || 0
    });
    ctx.success(result);
  }

  // 批量创建或更新学生
  async bulkCreateOrUpdate() {
    const { ctx } = this;
    const students = ctx.request.body;

    if (!Array.isArray(students)) {
      ctx.throw(400, '请求数据格式错误，应为数组');
    }

    // 验证必填字段
    for (const student of students) {
      const { student_no, name, gender, grade, class_name } = student;
      console.log('student_no, name, gender, grade, class_name',student_no, name, gender, grade, class_name)
      if (!student_no || !name || !gender || !grade || !class_name) {
        ctx.throw(400, '学号、姓名、性别、年级、班级为必填项');
      }
    }

    const result = await this.ctx.service[this.serviceName].bulkCreateOrUpdate(students);
    ctx.success(result);
  }

  // 获取班级列表
  async getClassList() {
    const { ctx } = this;
    const query = ctx.request.query
    const result = await this.ctx.service[this.serviceName].getClassList(query);
    ctx.success(result);
  }

  // 根据学号查询学生
  async findByStudentNo() {
    const { ctx } = this;
    const { student_no } = ctx.query;
    if (!student_no) {
      ctx.throw(400, '学号不能为空');
    }
    const result = await this.show(student_no);
    ctx.success(result);
  }

  // 绑定学生的家长
  async bindStudentParent() {
    const { ctx } = this;
    const { student_no, grade, class_name, parent_id, name } = ctx.request.body;
    if (!student_no || !grade || !class_name || !parent_id || !name) {
      ctx.throw(400, '、姓名、学号、年级、班级、家长为必填项');
    }
    // 检查学生是否存在
    const student = await this.ctx.service[this.serviceName].findOneWithWhere({
      student_no,
      grade,
      class_name,
      name
    });
    if (!student) {
      ctx.throw(400, '未找到该学生');
    }
    // 检查该学生是否绑定家长
    if (student.parent_id) {
      ctx.throw(400, '该学生已绑定家长，请联系家长解绑');
    }
    // 检查家长是否存在
    const parent = await this.ctx.service.parent.findOne(parent_id);
    if (!parent) {
      ctx.throw(400, '家长不存在');
    }
    const result = await this.ctx.service[this.serviceName].update(student.id, {
      parent_id
    });
    ctx.success(result);
  }
  // 解绑学生家长
  async unbindStudentParent() {
    const { ctx } = this;
    const { student_id } = ctx.request.body;
    if (!student_id) {
      ctx.throw(400, '学生ID为必填项');
    }
    // 检查学生是否存在
    const student = await this.ctx.service[this.serviceName].findOne(student_id);
    if (!student) {
      ctx.throw(400, '未找到该学生');
    }
    // 检查该学生是否绑定家长
    if (!student.parent_id) {
      ctx.throw(400, '该学生未绑定家长');
    }
    const result = await this.ctx.service[this.serviceName].update(student.id, {
      parent_id: 0
    });
    ctx.success(result);
  }

  async deleteAll() {
    const { ctx } = this;
    const result = await this.ctx.service[this.serviceName].deleteAll();
    ctx.success(result);
  }
}

module.exports = StudentController;
